package cn.com.surker.material.controller;

import cn.com.surker.base.system.service.SysStorageService;
import cn.com.surker.base.system.vo.SysStorageVo;
import cn.com.surker.common.AbstractController;
import cn.com.surker.material.service.impl.MaterialHistoryInventoryServiceImpl;
import cn.com.surker.material.vo.MaterialHistoryInventoryVo;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author lucky
 * @create 2024年11月18 21:15
 * @desc
 */
@RestController
@RequestMapping("/material-history")
public class MaterialHistoryInventoryController extends AbstractController {

    @Resource
    private MaterialHistoryInventoryServiceImpl historyInventoryService;
    @Resource
    private SysStorageService storageService;
    @Value("${erp.datasource.erp.db}")
    protected String erpDb;
    @Value("${erp.org.id}")
    protected String erpOrgId;

    /**
     * 历史盘点分页查询
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResultDto page(String searchVal, Integer type, Long orgId, Long materialId, Long storageId, String startTime,
                              String endTime, Integer curr, Integer page, Integer limit){
        try {
            /**分页对象**/
            if (!StringUtils.isNull(curr))
                page = curr;
            else {
                if (StringUtils.isNull(page))
                    page = 1;
            }
            if (StringUtils.isNull(limit))
                limit = 15;
            IPage iPage = historyInventoryService.findByPage(searchVal, type, orgId, materialId, storageId, startTime,
                    endTime, (page - 1) * limit, limit);
            return RestResultDto.success(iPage);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,请稍后再试");
        }
    }

    /**
     * 更新库存信息
     * @param storageId
     * @param initial 期初值
     * @param storeValue 设定库存值
     * @return
     */
    @RequestMapping(value = "/update-dosage", method = RequestMethod.POST)
    public RestResultDto updateDosage(Long storageId, Double initial, Double storeValue){
        try {
            CacheUser user = getUser();
            Long orgId = StringUtils.convertLong(erpOrgId);
            return historyInventoryService.updateStoreDosage(orgId,storageId, initial, storeValue, user.getName(), 1);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,请稍后再试");
        }
    }
    /**
     * 超过一定时间未盘存预警
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/waring", method = RequestMethod.POST)
    public RestResultDto waringInventory(Long orgId){
        try {
            List<MaterialHistoryInventoryVo> inventoryVoList = historyInventoryService.waringInventory(erpDb, orgId);
            if (inventoryVoList == null)
                return RestResultDto.fail("没人任何数据");
            return RestResultDto.success(inventoryVoList);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,请稍后再试");
        }
    }
    /**
     * 实时库存
     * @return
     */
    @RequestMapping(value = "/real-time", method = RequestMethod.POST)
    public RestResultDto realTime(String time){
        try {
            Map<String, List<SysStorageVo>> map = storageService.realTimeStore(time, erpDb);
            if(map == null)
                return RestResultDto.fail("未获取到任何数据");
            return RestResultDto.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }

}
